package org.elfen.sys.proxy;

import org.elfen.common.util.DataUtil;
import org.elfen.sys.entity.SysUser;
import org.elfen.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 服务代理层：<br>
 * 	实现缓存和事务功能
 * 
 */
@Service
@CacheConfig(cacheNames="sysUser")
public class SysUserProxyService{
	@Autowired
	private SysUserService sysUserSerivce;

	@CachePut(key="'org.elfen.sys.proxy.SysUserProxyService.selectUserById'+#entity.id")
	public SysUser updaterById(SysUser entity) {
		sysUserSerivce.updateById(entity);
		return sysUserSerivce.selectById(entity.getId());
	}

	@Cacheable(key="'org.elfen.sys.proxy.SysUserProxyService.selectUserById'+#id",unless = "#result == null")
	public SysUser selectUserById(Long id) {
		System.out.println("==执行数据库查询==>[selectUserById]<=======");
		return sysUserSerivce.selectById(id);
	}

	@CacheEvict(key="'org.elfen.sys.proxy.SysUserProxyService.selectUserById'+#id")
	public boolean deleteById(Long id ,boolean flagDelete) {
		if(flagDelete){
			SysUser entity = new SysUser();
			entity.setId(id);
			entity.setIsDelete(1);
			System.out.println("==执行数据库删除==>ID="+entity.getId()+"，IsDelete="+entity.getIsDelete());
			return sysUserSerivce.updateById(entity);
		}
		return sysUserSerivce.deleteById(id);
	}

	public SysUser selectUserByUserName(String userName) {
		System.out.println("==执行数据库查询==>[selectUserByUserName]<=======");
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.eq("userName", userName);
		wrapper.eq("isDelete", 0);
		return sysUserSerivce.selectOne(wrapper);
	}
	
	public SysUser selectUserByNick(String nick) {
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.eq("userNick", nick);
		wrapper.eq("isDelete", 0);
		return sysUserSerivce.selectOne(wrapper);
	}

	public SysUser selectUserByQQ(String qq) {
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.eq("userQQ", qq);
		wrapper.eq("isDelete", 0);
		return sysUserSerivce.selectOne(wrapper);
	}

	public SysUser insertUser(SysUser sysUser) {
		sysUserSerivce.insert(sysUser);
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.eq("userName", sysUser.getUserName());
		return sysUserSerivce.selectOne(wrapper);
	}

	public SysUser selectUserByTabao(String taobao) {
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.eq("taobao", taobao);
		wrapper.eq("isDelete", 0);
		return sysUserSerivce.selectOne(wrapper);
	}

	public Page<SysUser> selectPage(Page<SysUser> page, SysUser user) {
		System.out.println("selectPage的user="+user);
		Page<SysUser> result = null;
		if(DataUtil.isEmpty(user)){
			result = sysUserSerivce.selectPage(page);
		}else{
			Wrapper<SysUser> wrapper = user.bindingQueryWrapper();
			result = sysUserSerivce.selectPage(page, wrapper);
		}
		return result;
	}

	@CacheEvict(allEntries=true)
	public void evictCache() {
		System.out.println("==[清空掉sysUser的所有缓存信息！！！]==");
	}

	public SysUser selectLastRegister() {
		Wrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
		wrapper.orderBy("createTime", false);
		return sysUserSerivce.selectOne(wrapper);
	}
}
